package com.cat.yygh.order.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cat.yygh.common.result.R;
import com.cat.yygh.common.util.JwtHelper;
import com.cat.yygh.enums.OrderStatusEnum;
import com.cat.yygh.model.order.OrderInfo;
import com.cat.yygh.order.service.OrderInfoService;
import com.cat.yygh.vo.order.OrderCountQueryVo;
import com.cat.yygh.vo.order.OrderQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2023-11-27
 */
@Api(tags = "订单接口")
@RestController
@RequestMapping("/api/order/orderInfo")
public class OrderInfoController {
    @Autowired
    private OrderInfoService orderInfoService;
    @ApiOperation(value = "获取订单统计数据")
    @PostMapping("/statistics")
    public Map<String,Object> statistics(@RequestBody OrderCountQueryVo orderCountQueryVo){
        return orderInfoService.statistics(orderCountQueryVo);
    }
    @ApiOperation(value = "创建订单")
    @PostMapping("/{scheduleId}/{patientId}")
    public R submitOrder(@ApiParam(name = "scheduleId", value = "排班id", required = true)
                         @PathVariable String scheduleId,
                         @ApiParam(name = "patientId", value = "就诊人id", required = true)
                         @PathVariable Long patientId){
        Long orderId = orderInfoService.submitOrder(scheduleId, patientId);
        return R.ok().data("orderId",orderId);
    }
    @ApiOperation(value = "根据订单id查询订单详情")
    @GetMapping("getOrders/{orderId}")
    public R getOrders(@PathVariable Long orderId) {
        OrderInfo orderInfo = orderInfoService.getOrderInfo(orderId);
        return R.ok().data("orderInfo",orderInfo);
    }
    //订单列表（条件查询带分页）
    @GetMapping("/{page}/{limit}")
    public R list(@PathVariable Long page,
                  @PathVariable Long limit,
                  OrderQueryVo orderQueryVo,
                  @RequestHeader String token) {
        //设置当前用户id
        Long userId = JwtHelper.getUserId(token);
        orderQueryVo.setUserId(userId);
        Page<OrderInfo> pageParam = new Page<>(page,limit);
        IPage<OrderInfo> pageModel = orderInfoService.selectPage(pageParam,orderQueryVo);
        return R.ok().data("pageModel",pageModel);
    }
    @ApiOperation(value = "获取订单状态")
    @GetMapping("/getStatusList")
    public R getStatusList() {
        return R.ok().data("statusList", OrderStatusEnum.getStatusList());
    }
    @ApiOperation(value = "取消预约")
    @GetMapping("/cancelOrder/{orderId}")
    public R cancelOrder(
            @ApiParam(name = "orderId",value = "订单id",required = true)
            @PathVariable("orderId") Long orderId){
        Boolean flag = orderInfoService.cancelOrder(orderId);
        return R.ok().data("flag",flag);
    }
}

